﻿using System;
using System.Collections.Generic;
using System.Linq;
using Nancy;
using Nancy.Routing;
using API_Service.BizLogic.Platform;

namespace API_Service.Module
{
    public class HomeModule : NancyModule
    {
        PlatformPubService pubService = new PlatformPubService();

        public HomeModule(IRouteCacheProvider routeCacheProvider)
        {
            Get["GetHome", "/"] = _ =>
            {
                return View["home"];
            };

            Get["/api"] = _ =>
            {
                return View["api"];
            };

            //Get["/largejson"] = _ =>
            //{
            //    List<dynamic> res = new List<dynamic>();

            //    var m = new
            //    {
            //        c1 = "dsadad",
            //        c2 = "dasdsad",
            //        c3 = "dasdsad",
            //        c4 = "大萨达所多",
            //        c5 = "大声道撒多",
            //        c6 = "839128481748214",
            //        c7 = "%……%……%",
            //        c8 = "大萨达撒多",
            //        c9 = "大萨达撒多撒多撒多"
            //    };

            //    for (int i = 0; i < 100000; i++)
            //    {
            //        res.Add(m);
            //    }

            //    return Response.AsJson(res);
            //};

            Get["/tokenview"] = _ => Response.AsJson(SCUDictionary.TokenList);

            Get["/client/upload/"] = _ =>
            {
                ViewBag.Message = "";

                return View["upload"];
            };

            Post["/client/upload/action"] = _ =>
            {
                IEnumerable<HttpFile> files = Request.Files;

                string resourceType = Request.Form["resourcetype"];

                ViewBag.Message = "文件传送成功. " + files.Count() + "  " + resourceType;

                return View["upload"];
            };

            Get["/clientsetup/{filename}"] = p =>
            {
                return Response.AsFile($"./content/clientsetup/{(string)p.filename}");
            };

            Get["/env/{filename}"] = p =>
            {
                return Response.AsFile($"./content/environment/{(string)p.filename}");
            };

            Get["/platform/token/{userid}/{password}"] = p =>
            {
                UserDentity user = new UserDentity()
                {
                    UserID = (string)p.userid,
                    Password = (string)p.password
                };

                return Response.AsJson(pubService.GetToken(user));
            };

            Get["/platform/getuserbytoken"] = p =>
            {
                string token = (string)Request.Query["token"];

                return Response.AsJson(pubService.GetUserByToken(token));
            };
        }
    }
}